﻿@model NextAction.WebUI.Models.GtdTaskIndexViewModel
           
@using NextAction.Domain.Entities

@{
    Layout = null;

    AjaxOptions ajaxOpts = new AjaxOptions
    {
        UpdateTargetId = "task_list",
        Url = Url.Action("GtdTaskListData", new { Model.BucketType }),
        OnSuccess = "IndexTaskView.onTaskCreation"
    };
}

@if (false) {
    <script src="../../Scripts/jquery-1.7-vsdoc.js" type="text/javascript"></script>
    <script src="~/Scripts/next.action-1.0.js" type="text/javascript"></script>
}

<script type="text/javascript">
    $(function () {
        IndexTaskView.refreshTaskList();
    })

    function IndexTaskView() {
    }

    IndexTaskView.refreshTaskList = function() {
        IndexTaskView.refreshTaskType();
        IndexTaskView.refreshTaskCount();
        IndexTaskView.enableTaskCompleting();
        IndexTaskView.enableTaskDragging();
        IndexTaskView.enableTaskEditing();
    }

    IndexTaskView.onTaskCreation = function() {
        $('#newTaskInput').val("");
        IndexTaskView.refreshTaskList();
        IndexCollectionView.refreshBucketsCount();
    }

    IndexTaskView.enableTaskDragging = function() {
        $('li.task_row').draggable({
            revert: true,
            revertDuration: '200',
            helper: 'clone', 
            cursorAt: { top: 10, left: 5} });
    }

    IndexTaskView.enableTaskCompleting = function() {
        $("input.complete_checkbox").click(function () {
            var taskID = $(this).parents('li.task_row').attr('task_id');
            GtdRepository.completeTask(taskID, function () {
                IndexCollectionView.loadTasksForSelectedBucket();
            });
        })
    }

    IndexTaskView.enableTaskEditing = function() {
        $('li.task_row a.task_link').click(function () {
            var taskID = $(this).parents('li.task_row').attr('task_id');
            EditTaskView.editTask(taskID);
        })
    }
    
    IndexTaskView.refreshTaskType =  function() {
        var taskType = $('#task_list_params').attr('task_type');
        var createTaskUrl = $('#task_list_params').attr('create_task_url');
        var taskDataUrl = $('#task_list_params').attr('task_data_url');

        $('#task_list_header').text(taskType);
        $('a#add_task').attr('href', createTaskUrl);
        $('form#form0').attr('data-ajax-url', taskDataUrl);
    }

    IndexTaskView.refreshTaskCount = function() {
        var taskCount = $('li.task_row').size();
        $('div#task_count').text(taskCount + ' task(s)');
    }
</script>


@using (Ajax.BeginForm("GtdTaskListData", ajaxOpts))
{
    <div class="post" id="post-28">

	    <div class="post_title_left">
	    <div class="post_title_right post_title_body">
		    <h1 class="left" id="task_list_header">
                @Model.BucketType.ToString()
            </h1>
            <div class="right">
                <span class="comment">
                    @Html.ActionLink("Add", "Create", "Task", new { Model.BucketType }, new { id = "add_task" })
                </span>
            </div>
		    <div class="clearer">&nbsp;</div>
        </div>
        </div>

	    <div class="post_body">
			    <table class="search">
			    <tr>
                    @Html.TextBox("newTaskName", "", new { @class = "text-box new-task-box", id = "newTaskInput" })
			    </tr>
			    </table>
	    </div>

	    <div class="post_bottom"></div>

    </div>

    <div class="post" id="comments">
					
        <div class="post_title_left">
	    <div class="post_title_right post_title_body">

		    <h1 class="left"></h1>
            <div class="post_date right" id="task_count">@Model.GtdTasks.Count() task(s)</div>
		    <div class="clearer">&nbsp;</div>
	    </div>
        </div>

        <div id="task_list" class="post_body nicelist">
            @Html.Action("GtdTaskListData", "Task", new { Model.BucketType })
	    </div>
    </div>

}
        

